package com.lznAdmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lznAdmin.base.common.util.QueryWrapperUtil;
import com.lznAdmin.base.lznAdminResult;
import com.lznAdmin.system.entity.SysDictMain;
import com.lznAdmin.system.entity.SysDictSub;
import com.lznAdmin.system.service.SysDictMainService;
import com.lznAdmin.system.service.SysDictSubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: lznAdmin-pro
 * @description:
 * @author: lzn
 * @create: 2022-01-24 16:33
 **/
@Slf4j
@Api(tags = "数据字典控制器")
@RestController
@RequestMapping("/sys/dict")
public class SysDictController {

    @Autowired
    private SysDictMainService sysDictMainService;
    @Autowired
    private SysDictSubService sysDictSubService;
    @Autowired
    private QueryWrapperUtil queryWrapperUtil;

    /**
     * 获取字典分页数据
     *
     * @param sysDictMain 字典主表实体
     * @param current     当前页
     * @param size        每页显示多少行
     * @param req         其他参数
     * @return
     */
    @ApiOperation(value = "获取字典分页数据", httpMethod = "GET", notes = "")
    @GetMapping("/list")
    public lznAdminResult getSysDictMainList(SysDictMain sysDictMain,
                                             @RequestParam(name = "current", defaultValue = "1") Integer current,
                                             @RequestParam(name = "size", defaultValue = "10") Integer size,
                                             HttpServletRequest req) {
        log.info("sysDictMain = {}", sysDictMain);
        Page<SysDictMain> page = new Page<>(current, size);
        Map<String, String[]> parameterMap = req.getParameterMap();
        // log.info(parameterMap.get("begin_date")[0].toString());
        // log.info(parameterMap.get("end_date")[0].toString());
        QueryWrapper<SysDictMain> sysDictMainQueryWrapper = queryWrapperUtil.BuildQueryWrapper(sysDictMain, req.getParameterMap());
        Page<SysDictMain> sysDictMainPage = sysDictMainService.page(page, sysDictMainQueryWrapper);
        log.info("sysDictMainResult = {}", sysDictMainPage.getRecords().toString());
        return lznAdminResult.ok(sysDictMainPage);
    }

    @ApiOperation(value = "新增数据字典主表数据", httpMethod = "POST", notes = "")
    @PostMapping("/add/main")
    public lznAdminResult addSysDictMain(@RequestBody SysDictMain sysDictMain) {
        log.info("SysDictMain = {}", sysDictMain);
        boolean isSave = sysDictMainService.save(sysDictMain);
        if (isSave) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "编辑数据字典主表数据", httpMethod = "PUT", notes = "")
    @PutMapping("/edit/main")
    public lznAdminResult editSysDictMain(@RequestBody SysDictMain sysDictMain) {
        log.info("edit-SysDictMain = {}", sysDictMain);
        boolean isUpdate = sysDictMainService.updateById(sysDictMain);
        if (isUpdate) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "删除数据字典主表数据", httpMethod = "DELETE", notes = "")
    @DeleteMapping("/delete/main")
    public lznAdminResult deleteSysDictMain(@RequestBody SysDictMain sysDictMain) {
        log.info("delete-SysDictMain = {}", sysDictMain);
        boolean isRemove = sysDictMainService.removeById(sysDictMain);
        if (isRemove) {
            return lznAdminResult.ok("删除成功！");
        }
        return lznAdminResult.no("删除失败！");
    }

    /**
     * 获取字典明细分页数据
     *
     * @param sysDictSub 字典子表实体
     * @param current    当前页
     * @param size       每页显示多少行
     * @param req        其他参数
     * @return
     */
    @ApiOperation(value = "获取字典字表分页数据", httpMethod = "GET", notes = "")
    @GetMapping("/list/sub")
    public lznAdminResult getSysDictSubList(SysDictSub sysDictSub,
                                            @RequestParam(name = "current", defaultValue = "1") Integer current,
                                            @RequestParam(name = "size", defaultValue = "10") Integer size,
                                            HttpServletRequest req) {
        log.info("sysDictSub = {}", sysDictSub);
        Page<SysDictSub> page = new Page<>(current, size);
        Map<String, String[]> parameterMap = req.getParameterMap();
        // log.info(parameterMap.get("begin_date")[0].toString());
        // log.info(parameterMap.get("end_date")[0].toString());
        QueryWrapper<SysDictSub> sysDictSubQueryWrapper = queryWrapperUtil.BuildQueryWrapper(sysDictSub, req.getParameterMap());
        Page<SysDictSub> sysDictSubPage = sysDictSubService.page(page, sysDictSubQueryWrapper);
        log.info("sysDictSubResult = {}", sysDictSubPage.getRecords().toString());
        return lznAdminResult.ok(sysDictSubPage);
    }

    @ApiOperation(value = "新增数据字典子表数据", httpMethod = "POST", notes = "")
    @CacheEvict(value = "sys:cache:dict", allEntries = true)
    @PostMapping("/add/sub")
    public lznAdminResult addSysDictSub(@RequestBody SysDictSub sysDictSub) {
        log.info("SysDictSub = {}", sysDictSub);
        boolean isSave = sysDictSubService.save(sysDictSub);
        if (isSave) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "编辑数据字典子表数据", httpMethod = "PUT", notes = "")
    @CacheEvict(value = "sys:cache:dict", allEntries = true)
    @PutMapping("/edit/sub")
    public lznAdminResult editSysDictSub(@RequestBody SysDictSub sysDictSub) {
        log.info("edit-SysDictSub = {}", sysDictSub);
        boolean isUpdate = sysDictSubService.updateById(sysDictSub);
        if (isUpdate) {
            return lznAdminResult.ok("保存成功！");
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "删除数据字典子表数据", httpMethod = "DELETE", notes = "")
    @CacheEvict(value = "sys:cache:dict", allEntries = true)
    @DeleteMapping("/delete/sub")
    public lznAdminResult deleteSysDictSub(@RequestBody SysDictSub sysDictSub) {
        log.info("delete-SysDictSub = {}", sysDictSub);
        boolean isRemove = sysDictSubService.removeById(sysDictSub);
        if (isRemove) {
            return lznAdminResult.ok("删除成功！");
        }
        return lznAdminResult.no("删除失败！");
    }
}
